﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using KimPhongCRM.Models;
using DevExpress.Web.Mvc;

namespace KimPhongCRM.Controllers
{
    public class RoleController : Controller
    {
        private Entities db = new Entities();

        //
        // GET: /Role/

        public ViewResult Index()
        {
            var roles = db.Roles.Include(r => r.Application);
            return View(roles.ToList());
        }

        //
        // GET: /Role/Details/5

        public ViewResult Details(Guid id)
        {
            Role role = db.Roles.Find(id);
            return View(role);
        }

        //
        // GET: /Role/Create

        public ActionResult Create()
        {
            ViewBag.ApplicationId = new SelectList(db.Applications, "ApplicationId", "ApplicationName");
            return View();
        }

        //
        // POST: /Role/Create

        [HttpPost]
        public ActionResult Create(Role role)
        {
            if (ModelState.IsValid)
            {
                role.RoleId = Guid.NewGuid();
                db.Roles.Add(role);
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            ViewBag.ApplicationId = new SelectList(db.Applications, "ApplicationId", "ApplicationName", role.ApplicationId);
            return View(role);
        }

        //
        // GET: /Role/Edit/5

        public ActionResult Edit(Guid id)
        {
            Role role = db.Roles.Find(id);
            ViewBag.ApplicationId = new SelectList(db.Applications, "ApplicationId", "ApplicationName", role.ApplicationId);
            return View(role);
        }

        //
        // POST: /Role/Edit/5

        [HttpPost]
        public ActionResult Edit(Role role)
        {
            if (ModelState.IsValid)
            {
                db.Entry(role).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            ViewBag.ApplicationId = new SelectList(db.Applications, "ApplicationId", "ApplicationName", role.ApplicationId);
            return View(role);
        }

        //
        // GET: /Role/Delete/5

        public ActionResult Delete(Guid id)
        {
            Role role = db.Roles.Find(id);
            return View(role);
        }

        //
        // POST: /Role/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(Guid id)
        {
            Role role = db.Roles.Find(id);
            db.Roles.Remove(role);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
        KimPhongCRM.Models.Entities db1 = new KimPhongCRM.Models.Entities();

        [ValidateInput(false)]
        public ActionResult ListRolePartial()
        {
            var model = db1.Roles;
            return PartialView("_ListRolePartial", model.ToList());
        }

        [HttpPost, ValidateInput(false)]
        public ActionResult ListRolePartialAddNew(KimPhongCRM.Models.Role item)
        {
            var model = db1.Roles;
            if (ModelState.IsValid)
            {
                try
                {
                    model.Add(item);
                    db1.SaveChanges();
                }
                catch (Exception e)
                {
                    ViewData["EditError"] = e.Message;
                }
            }
            else
                ViewData["EditError"] = "Please, correct all errors.";
            return PartialView("_ListRolePartial", model.ToList());
        }
        [HttpPost, ValidateInput(false)]
        public ActionResult ListRolePartialUpdate(KimPhongCRM.Models.Role item)
        {
            var model = db1.Roles;
            if (ModelState.IsValid)
            {
                try
                {
                    var modelItem = model.FirstOrDefault(it => it.RoleId == item.RoleId);
                    if (modelItem != null)
                    {
                        this.UpdateModel(modelItem);
                        db1.SaveChanges();
                    }
                }
                catch (Exception e)
                {
                    ViewData["EditError"] = e.Message;
                }
            }
            else
                ViewData["EditError"] = "Please, correct all errors.";
            return PartialView("_ListRolePartial", model.ToList());
        }
        [HttpPost, ValidateInput(false)]
        public ActionResult ListRolePartialDelete(System.Guid RoleId)
        {
            var model = db1.Roles;
            if (RoleId != null)
            {
                try
                {
                    var item = model.FirstOrDefault(it => it.RoleId == RoleId);
                    if (item != null)
                        model.Remove(item);
                    db1.SaveChanges();
                }
                catch (Exception e)
                {
                    ViewData["EditError"] = e.Message;
                }
            }
            return PartialView("_ListRolePartial", model.ToList());
        }
    }
}